import 'package:chathouse/pages/route_manager/root_route_manager.dart';
import 'package:chathouse/public/Config.dart';
import 'package:chathouse/util/base_utils.dart';
import 'package:chathouse/util/log.dart';
import 'package:ff_stars/ff_stars.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../model/response_bean.dart';
import '../../../public/event_bus.dart';

/// @class : CommentOrderPage
/// @date : 2023-02-28 17:12
/// @name : LFC
/// @description :擬寫評語

class CommentOrderPage extends StatefulWidget {
  var projectId;
  var isShowBottomBar;

  CommentOrderPage(this.projectId, {Key? key, this.isShowBottomBar})
      : super(key: key);

  @override
  State<CommentOrderPage> createState() => _CommentOrderPageState();
}

class _CommentOrderPageState extends State<CommentOrderPage>
    with NavigationHelper {
  TextEditingController controller = TextEditingController();
  var selectedStars = 0.0;

  @override
  void initState() {
    controller.text = "";
    super.initState();
  }

  Future<bool> _requestPop() {
    confirmExit();
    return Future.value(false);
  }

  void confirmExit() {
    Get.dialog(
        SimpleDialog(
          titlePadding: EdgeInsets.zero,
          contentPadding: EdgeInsets.zero,
          insetPadding: EdgeInsets.symmetric(horizontal: 30.w),
          backgroundColor: Colors.transparent,
          children: [
            Container(
              padding: EdgeInsets.only(top: 42.w),
              alignment: Alignment.center,
              height: 266.w,
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.circular(26.w)),
              child: Column(
                children: [
                  Text(
                    "確定離開?",
                    style: getTextStyle(30.sp,
                        fontType: FontType.BOLD, color: Colors.black),
                  ),
                  SizedBox(height: 10.w),
                  ImageUtils.image(Res.icon_danger, width: 60.w, height: 54.w),
                  Row(
                    children: [
                      Expanded(
                        child: Container(
                          margin: EdgeInsets.only(left: 10.w, right: 10.w),
                          padding: EdgeInsets.only(top: 6.w, bottom: 6.w),
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                              color: MyColors.gary_214214214,
                              borderRadius: BorderRadius.circular(24.w)),
                          child: Text(
                            "離開",
                            style: getTextStyle(20.sp,
                                color: MyColors.red_2556161,
                                fontType: FontType.BOLD),
                          ),
                        ).setOnClickListener(() {
                          Get.back();
                          Get.back();
                          back();
                        }, isInkWell: true),
                      ),
                      Expanded(
                        child: Container(
                          margin: EdgeInsets.only(left: 10.w, right: 10.w),
                          padding: EdgeInsets.only(top: 6.w, bottom: 6.w),
                          alignment: Alignment.center,
                          decoration: BoxDecoration(
                              color: MyColors.zo_25118177,
                              borderRadius: BorderRadius.circular(24.w)),
                          child: Text(
                            "繼續",
                            style: getTextStyle(20.sp,
                                color: Colors.white, fontType: FontType.BOLD),
                          ),
                        ).setOnClickListener(() {
                          Get.back();
                          // Get.back();
                          // back();
                        }, isInkWell: true),
                      ),
                    ],
                  ).setContainer(margin: EdgeInsets.only(top: 50.w))
                ],
              ),
            )
          ],
        ),
        barrierDismissible: false,
        barrierColor: const Color.fromRGBO(0, 0, 0, 0.75));
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: _requestPop,
      child: Scaffold(
        resizeToAvoidBottomInset: false,
        backgroundColor: Colors.white,
        appBar: AppBar(
          backgroundColor: Colors.white,
          shadowColor: Colors.transparent,
          leading: Icon(
            Icons.arrow_back_ios,
            size: 28.w,
            color: MyColors.gary_149149149,
          ).setOnClickListener(() {
            confirmExit();
          }),
          centerTitle: true,
          title: Text(
            "擬寫評語",
            textAlign: TextAlign.center,
            style: getTextStyle(28.sp,
                color: Colors.black, fontType: FontType.BOLD),
          ),
        ),
        body: ListView(
          children: [
            Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.start,
                  children: [
                    Text("評分",
                        style: getTextStyle(28.sp,
                            color: MyColors.black_0000011,
                            fontType: FontType.BOLD)),
                    SizedBox(
                      width: 12.w,
                    ),
                    FFStars(
                      normalStar: const Icon(Icons.star,
                          size: 40, color: MyColors.gary_214214214),
                      selectedStar: const Icon(Icons.star,
                          size: 40, color: MyColors.red_2166474),
                      starsChanged: (realStars, selectedStars) {
                        this.selectedStars = realStars;
                        LogUtil.d("real: $selectedStars, final: $realStars");
                      },
                      step: 1,
                      defaultStars: 0,
                      starHeight: 40,
                      starWidth: 40,
                      starMargin: 8.w,
                    ),
                  ],
                ),
                SizedBox(
                  height: 40.w,
                ),
                Text("評語",
                    style: getTextStyle(28.sp,
                        color: MyColors.black_0000011,
                        fontType: FontType.BOLD)),
                SizedBox(
                  height: 16.w,
                ),
                TextField(
                  controller: controller,
                  style: getTextStyle(16.sp,
                      color: Colors.black, fontType: FontType.MID),
                  maxLines: 5,
                  decoration: InputDecoration(
                    hintText: "",
                    hintStyle: getTextStyle(16.sp,
                        color: MyColors.gary_210210210, fontType: FontType.MID),
                    border: InputBorder.none,
                  ),
                ).setContainer(
                  margin: EdgeInsets.only(right: 20.w),
                  padding: EdgeInsets.only(left: 12.w),
                  height: 168,
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(10.w),
                    border:
                        Border.all(color: MyColors.gary_158157157, width: 1.w),
                  ),
                ),
                SizedBox(
                  height: 26.w,
                ),
                Center(
                  child: Text("提交評語",
                          style: getTextStyle(25.sp,
                              color: Colors.white, fontType: FontType.BOLD))
                      .setContainer(
                          alignment: Alignment.center,
                          padding: EdgeInsets.symmetric(horizontal: 36.w),
                          height: 52.w,
                          width: 178.w,
                          decoration: BoxDecoration(
                            color: MyColors.green_69157608,
                            borderRadius: BorderRadius.circular(32.w),
                            border: Border.all(
                                color: MyColors.gary_158157157, width: 1.w),
                          ))
                      .setOnClickListener(() {
                    commitData();
                  }, isInkWell: true),
                ),
              ],
            ).setContainer(padding: EdgeInsets.only(left: 30.w)),
          ],
        ),
      ),
    );
  }

  commitData() {
    if (selectedStars == 0) {
      ToastUtil.showToast("請先打評分");
      return;
    }

    Obj.showLoading();
    final params = {
      "projectId": widget.projectId,
      "serviceEvaluation": selectedStars,
      "comUser": controller.text.toString(),
    };
    Apis.commonExpert(params).then((value) {
      Obj.dismissLoading();
      FocusManager.instance.primaryFocus?.unfocus();
      var result = ResponseBean.fromJson(value?.data ?? {});
      LogUtil.d("contact success $value");
      if (result.isRequestSuccess) {
        Get.dialog(
            SimpleDialog(
              titlePadding: EdgeInsets.zero,
              contentPadding: EdgeInsets.zero,
              insetPadding: EdgeInsets.symmetric(horizontal: 30.w),
              backgroundColor: Colors.transparent,
              children: [
                Container(
                  padding: EdgeInsets.only(top: 96.w),
                  alignment: Alignment.center,
                  height: 266.w,
                  decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(26.w)),
                  child: Column(
                    children: [
                      Text(
                        "多謝你的寶貴意見",
                        style: getTextStyle(32.sp,
                            fontType: FontType.BOLD, color: Colors.black),
                      ),
                      Container(
                        width: double.infinity,
                        margin:
                            EdgeInsets.only(left: 60.w, right: 60.w, top: 60.w),
                        padding: EdgeInsets.only(top: 6.w, bottom: 6.w),
                        alignment: Alignment.center,
                        decoration: BoxDecoration(
                            color: MyColors.green_69157608,
                            borderRadius: BorderRadius.circular(24.w)),
                        child: Text(
                          "關閉",
                          style: getTextStyle(28.sp,
                              color: Colors.white, fontType: FontType.BOLD),
                        ),
                      ).setOnClickListener(() {
                        Get.back();
                        Get.back();
                        back();
                        //跳轉我的評語頁面
                        eventBus.fire(ToOrderEvent());
                        // eventBus.fire(UpdateOrderListEvent());
                      }, isInkWell: true)
                    ],
                  ),
                )
              ],
            ),
            barrierDismissible: false,
            barrierColor: const Color.fromRGBO(0, 0, 0, 0.75));
      }
    }).catchError((error) {
      LogUtil.d("commonExpert error $error");
      Obj.dismissLoading();
    });
  }
}
